package com.web.text;

import com.web.text.diff.StringComparatorUtil;
import com.web.text.split.StringSplitUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringUtil {
    public static final List<String> BR_LIST = Arrays.asList("</p>", "</div>", "<br/>", "<br>", "</h1>", "</h2>", "</h3>", "</h4>", "</h5>", "</h6>");

    private StringUtil() {
    }


    /**
     * 判断index  是否在list范围中
     *
     * @param list  有序范围集合
     * @param index 当前位置
     * @return
     */
    public static boolean isRange(List<int[]> list, int index) {
        if (null == list || list.isEmpty()) {
            return false;
        }
        int center = list.size() / 2;
        int[] ints = list.get(center);
        int begin = 0;
        int end = 0;
        if (index < ints[0]) {
            begin = 0;
            end = center;
        } else if (index >= ints[1]) {
            begin = center;
            end = list.size();
        } else {
            return true;
        }
        for (int i = begin; i < end; i++) {
            int[] ints1 = list.get(i);
            if (index >= ints1[0] && index < ints1[1]) {
                return true;
            }
        }
        return false;
    }


    /**
     * 字符串对比
     *
     * @param before       之前
     * @param after        之后
     * @param cleanHtmlTag 是否去掉标签
     * @param addBegin     添加内容开始标签
     * @param addEnd       添加内容结束标签
     * @param delBegin     删除内容开始标签
     * @param delEnd       删除内容结束标签
     * @return 返回对比结果
     */
    public static String comparator(String before, String after, boolean cleanHtmlTag, String addBegin, String addEnd, String delBegin, String delEnd) {
        return StringComparatorUtil.comparator(before, after, cleanHtmlTag, addBegin, addEnd, delBegin, delEnd);
    }


    /**
     * 截取字符串
     *
     * @param content
     * @param size    多少个字符
     * @return
     */
    public static String split(String content, int size) {
        return StringSplitUtil.split(content, size);
    }


    /**
     * 带html内容的分段操作
     *
     * @param content
     * @return
     */
    public static List<String> subsection(String content) {
        int size = content.length();
        int i = 0;
        int j = 0;
        StringBuilder sb = new StringBuilder();
        List<String> list = new ArrayList<>();
        while (i < size) {
            Character c = content.charAt(i);
            i++;
            sb.append(c);
            if (c == '<') {
                j = 0;
            } else {
                j++;
            }
            if (j > 2 && j < 6) {
                boolean isBr = false;
                String str1 = sb.toString();
                for (String str : BR_LIST) {
                    if (str1.endsWith(str)) {
                        isBr = true;
                        break;
                    }
                }
                if (isBr) {
                    list.add(str1);
                    sb = new StringBuilder();
                }
            }
        }
        return list;
    }
}
